package cc.lj.internal.payment.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.payment.domain.pay.repository.ITicketPayRecordRepository;
import cc.lj.internal.payment.domain.settlement.repository.ICouponsSubsidyDetailRepository;
import cc.lj.internal.payment.infrastructure.dao.ICouponsSubsidyDetailMapper;
import cc.lj.internal.payment.infrastructure.dao.ITicketPayRecordMapper;
import cc.lj.internal.payment.infrastructure.exception.PaymentExceptionEnum;
import cc.lj.internal.payment.infrastructure.po.CouponsSubsidyDetailPO;
import cc.lj.internal.payment.infrastructure.po.TicketPayRecordPO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 电子券分摊明细表　mysql仓储实现
 * @author liangxifeng
 * @date 2022-10-24
 */
@Repository("couponsSubsidyDetailMysqlRespository")
public class CouponsSubsidyDetailMysqlRespository implements ICouponsSubsidyDetailRepository,BasePageHelper<CouponsSubsidyDetailPO> {
    @Autowired
    private ICouponsSubsidyDetailMapper mapper;
    @Override
    public List list(CouponsSubsidyDetailPO entityPO) {
        QueryWrapper<CouponsSubsidyDetailPO> wrapper = new QueryWrapper<>(entityPO);
        List<CouponsSubsidyDetailPO> entitys = mapper.selectList(wrapper);
        return entitys;
    }

    @Override
    public Integer updateById(CouponsSubsidyDetailPO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(PaymentExceptionEnum.UPDATE_COUPONS_SUBSIDY_DETAIL_FAILED);
        }
        return res;
    }

    @Override
    public PageInfo<CouponsSubsidyDetailPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<CouponsSubsidyDetailPO> getList(CouponsSubsidyDetailPO subsidyDetailPO) {
        return this.list(subsidyDetailPO);
    }

    @Override
    public Integer insert(CouponsSubsidyDetailPO subsidyDetailPO) {
        Integer rows = mapper.insert(subsidyDetailPO);
        if(rows != 1) {
            throw new ServiceException(PaymentExceptionEnum.INSERT_COUPONS_SUBSIDY_DETAIL_FAILED);
        }
        return subsidyDetailPO.getId();
    }

    @Override
    public BigDecimal getMartSharePriceSum(CouponsSubsidyDetailPO detailPO) {
        LambdaQueryWrapper<CouponsSubsidyDetailPO> wrapper = new LambdaQueryWrapper();
        //商户展位号
        if(detailPO.getMerchantShow() != null ) {
            wrapper.eq(CouponsSubsidyDetailPO::getMerchantShow, detailPO.getMerchantShow());
        }
        //封顶额活动主键
        if(detailPO.getCapId() != null) {
            wrapper.eq(CouponsSubsidyDetailPO::getCapId, detailPO.getCapId());
        }
        //电子券批次主键
        if(detailPO.getBatchId() != null) {
            wrapper.eq(CouponsSubsidyDetailPO::getBatchId, detailPO.getBatchId());
        }
        // 分摊类型
        if( detailPO.getShareType() != null) {
            wrapper.eq(CouponsSubsidyDetailPO::getShareType, detailPO.getShareType());
        }
        BigDecimal sum = mapper.martSharePriceSum(wrapper) ;
        return sum == null ? new BigDecimal(0) : sum;
    }

    @Override
    public CouponsSubsidyDetailPO getById(Integer id) {
        return mapper.selectById(id);
    }

}
